package com.foreknow.view;

import com.foreknow.model.Admin;
import com.foreknow.service.AdminService;
import com.foreknow.service.impl.AdminServiceImpl;
import com.foreknow.view.Login;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;

public class Register extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JPasswordField passwordField;
    private JPasswordField passwordField_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Register frame = new Register();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Register() {
        setTitle("\u6CE8\u518C");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 788, 481);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblNewLabel = new JLabel("\u7528\u6237\u540D");
        lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        lblNewLabel.setBounds(128, 114, 72, 18);
        contentPane.add(lblNewLabel);

        JLabel label = new JLabel("\u5BC6\u7801");
        label.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        label.setBounds(128, 179, 72, 18);
        contentPane.add(label);

        JLabel label_1 = new JLabel("\u8BF7\u786E\u8BA4\u5BC6\u7801");
        label_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        label_1.setBounds(117, 241, 107, 18);
        contentPane.add(label_1);

        textField = new JTextField();
        textField.setBounds(265, 109, 226, 33);
        contentPane.add(textField);
        textField.setColumns(10);

        JButton btnNewButton = new JButton("\u6CE8\u518C");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Admin admin=new Admin();
                String username = textField.getText();
                String password = passwordField.getText();
                String repassword=passwordField_1.getText();
                if(password.equals(repassword)){
                    admin.setAdminName(username);
                    admin.setPassword(password);
                    AdminService adminService = new AdminServiceImpl();
                    boolean login = adminService.register(admin);
                    if(login){
                        System.out.println("注册成功");
                        Login login1=new Login();
                        login1.setVisible(true);
                        Register.this.dispose();
                    }

                }
            }
        });
        btnNewButton.setFont(new Font("宋体", Font.PLAIN, 19));
        btnNewButton.setBounds(179, 321, 100, 51);
        contentPane.add(btnNewButton);

        JButton button = new JButton("\u53D6\u6D88");
        button.setFont(new Font("宋体", Font.PLAIN, 19));
        button.setBounds(444, 321, 100, 51);
        contentPane.add(button);

        passwordField = new JPasswordField();
        passwordField.setBounds(265, 178, 226, 33);
        contentPane.add(passwordField);

        passwordField_1 = new JPasswordField();
        passwordField_1.setBounds(265, 240, 226, 33);
        contentPane.add(passwordField_1);
    }

}
